'use strict'
var config = require('./config')
var Wheel = require('./wheel')
var User = require('./user')
var users = {}
var wheels = {}

function init(){
    var r = config.START_R
    var step = config.STEP
    var current_wheel
    var old_wheel
    for(var i = 0; i < config.MAX_WHEELS; i++){
        current_wheel = new Wheel(r, i)
        if(old_wheel){
            current_wheel['pre'] = old_wheel
            old_wheel['next'] = current_wheel
        }
        old_wheel = current_wheel
        wheels[i] = old_wheel
        wheels[i].openHole(i + 1)
        step += config.STEP
        r += step
    }
    for(var i = 0; i < config.START_USERS; i++){
        users[i] = new User(i)
        wheels[0].getIn(users[i])
    }
}


function round(){
    for(var i in users){
        users[i].setVote(Math.floor(Math.random()* 10))
        users[i].hole(Math.floor(Math.random()* 2))
    }
    var num = 0
    for(var i in wheels){
        
        wheels[i].round()
        num += wheels[i].users
        console.log(i + '层总长: ' + wheels[i].units + ' 洞: ' + wheels[i].holes + ' 共: ' + wheels[i].users + '人  总投票: ' + wheels[i].votes)
    }
    console.log('局中共: ' + num + '人')
    if(num != config.START_USERS){
        for(var i in wheels){
            console.log(wheels[i].hole.join(''))
        }
        return
    }
    setTimeout(round, config.TIME_OUT * 1000)
}

init()
round()